﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using ServiceStack.Redis;

namespace QuotationService.RealtimeQuotation
{
    /// <summary>
    /// 市场信息通道
    /// </summary>
    [DebuggerNonUserCode]
    internal static class MarketInfoChannel
    {
        /// <summary>
        /// 发布市场信息
        /// </summary>
        /// <param name="source"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool Update(QuotationSource source, byte[] data)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (data == null || data.Length == 0)
                throw new ArgumentNullException("data");
            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            
            if (publisher == null)
                return false;

            using (new ProcessTime("Update MarketInfo", 1))
            {
                return publisher.Update(source.ToFormatString() + ":" + QuotationDataType.MarketInfo.ToFormatString(), data);
            }
        }

        /// <summary>
        /// 发布市场信息
        /// </summary>
        /// <param name="source"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static bool Publish(QuotationSource source, byte[] data)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            if (data == null || data.Length == 0)
                throw new ArgumentNullException("data");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            using (new ProcessTime("Publish MarketInfo", 1))
            {
                return publisher.Publish(source.ToFormatString() + ":" + QuotationDataType.MarketInfo.ToFormatString(), data);
            }
        }

        /// <summary>
        /// 查询市场的基本信息
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static byte[] Get(QuotationSource source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            using (var conn = QuotationSubscribeServiceContainer.GetNeedDisposedConnection(source))
            {
                if (conn == null)
                    return null;

                using (new ProcessTime("Get MarketInfo", 1))
                {
                    return (conn as RedisClient).Get(source.ToFormatString() + ":" + QuotationDataType.MarketInfo.ToFormatString());
                }
            }
        }

        /// <summary>
        /// 删除市场信息
        /// </summary>
        /// <param name="source"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static bool Clear(QuotationSource source)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            var publisher = QuotationPublishServiceContainer.GetPublishService(source);
            if (publisher == null)
                return false;

            using (new ProcessTime("Clear MarketInfo", 1))
            {
                return publisher.Delete(string.Format("{0}:{1}", source.ToFormatString(), QuotationDataType.MarketInfo.ToFormatString()));
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string ToSubscribeChannel(QuotationSource source)
        {
            return source.ToFormatString() + ":" + QuotationDataType.MarketInfo.ToFormatString();
        }
    }
}
